<template>
  <div>
    <div class="box">
      <a-icon type="left" @click="navPrev"/>
      <div class="box-item" ref="dataNavList">
        <div @click="clickClass(item)" class="item" :class="item.className" v-for="(item, index) in monthArr"
             :key="index"
             :style="{transform:'translateX(-'+move+'px)'}">
          <div class="year"> {{item.year}}</div>
          <div class="month"><span>{{item.month}}</span>月</div>
        </div>
      </div>
      <a-icon type="right" @click="navNext"/>
    </div>
  </div>
</template>

<script>
  import moment from "moment";

  moment.locale('zh-cn')

  export default {
    name: "SelectMonth",
    data() {
      return {
        monthArr: [],
        move: 0,
        dataNavListRealWidth: 0,
        dataNavListViewWidth: 0,
      }
    },
    created() {
      this.getHalfYear()
    },
    mounted() {
      this.getWindowSize()
    },
    methods: {
      getYearMonth() {
        return this.monthArr[0]
      },
      getWindowSize() {
        this.dataNavListRealWidth = this.$refs.dataNavList.scrollWidth;
        // offsetWidth获取菜单在当前页面可视宽度
        this.dataNavListViewWidth = this.$refs.dataNavList.offsetWidth;
        //实际宽度减去可视宽度就是可移动的范围即move的范围
        // 窗口大小变化时触发。实时更新可视宽度
        window.onresize = () => {
          return (() => {
            this.dataNavListViewWidth = this.$refs.dataNavList.offsetWidth;
            //注1，详见下方
            if (this.move > this.dataNavListRealWidth - this.dataNavListViewWidth - 75) {
              this.move = this.dataNavListRealWidth - this.dataNavListViewWidth;
            }
          })()
        }
      },
      clickClass(item) {
        for (let item of this.monthArr) {
          item.className = ''
        }
        item.className = 'clickClass'
        this.$emit('OnYearMonth', item)
      },
      navPrev() {
        if (this.move > 75) {
          this.move = this.move - 75;
        } else {
          this.move = 0
        }
      },
      // 当菜单项向左的可移动距离大于单个菜单项的宽度时，move减去一个菜单项的宽度（即左移移动一个菜单项的宽度），
      // 当菜单项向左的可移动距离小于单个菜单项的宽度时，move等于可以向左移动的最大值（即到最末尾）
      navNext() {
        if (this.move < this.dataNavListRealWidth - this.dataNavListViewWidth - 75) {
          this.move = this.move + 75;
        } else {
          this.move = this.dataNavListRealWidth - this.dataNavListViewWidth
        }
      },
      getHalfYear() {
        this.monthArr = []
        let yearMonth = ''
        for (let i = 0; i <= 11; i++) {
          yearMonth = moment().subtract(i, 'months').format('YYYY-M')
          let yearMonthArr = yearMonth.split('-')
          let yearMonthObj = {}
          if (i === 0) {
            yearMonthObj = {year: yearMonthArr[0], month: yearMonthArr[1], className: 'clickClass'}
          } else {
            yearMonthObj = {year: yearMonthArr[0], month: yearMonthArr[1], className: ''}
          }
          this.monthArr.push(yearMonthObj)
        }
      }
    }

  }
</script>

<style lang="less" scoped>
  .box {

    ::-webkit-scrollbar {
      /*滚动条整体样式*/
      width: 4px; /*高宽分别对应横竖滚动条的尺寸*/
      height: 1px;
    }

    ::-webkit-scrollbar-thumb {
      /*滚动条里面小方块*/
      background-color: rgba(0, 0, 0, 0);
    }

    ::-webkit-scrollbar-track {
      /*滚动条里面轨道*/
      background-color: rgba(0, 0, 0, 0);
    }

    box-shadow: 0 0 5px 2px #bec1e9 inset;
    padding: 0 5px;
    height: 80px;
    background-color: #e0eaff;
    display: flex;
    flex-direction: row;
    justify-content: space-evenly;
    align-items: center;

    .box-item {
      width: 90%;
      overflow-x: auto;
      overflow-y: hidden;
      white-space: nowrap;

      .clickClass {
        border: 1px solid #213e9a !important;

        .year {
          color: #213e9a !important;
        }

        .month {
          color: #213e9a !important;

          span {
            color: #213e9a !important;
          }
        }
      }

      .item {
        cursor: pointer;
        border: 1px dashed #7a9aff;
        border-radius: 5px;
        background-color: #fff;
        margin: 0 10px;
        width: 55px;
        height: 60px;
        display: inline-block;
        transition: transform .2s;

        .year {
          text-align: center;
          height: 30px;
          line-height: 30px;
          font-size: 16px;
        }

        .month {
          text-align: center;
          height: 25px;
          line-height: 25px;

          span {
            color: #9e9e9e;
            font-weight: bold;
            font-size: 22px;
          }
        }
      }
    }
  }
</style>